﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Plot
{
	/// <summary>
	/// C# Chart 控件绘图例子
	/// </summary>
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 清除所有图形
		/// </summary>
		private void ClearChart()
		{
			chart1.Series.Clear();
		}

		/// <summary>
		/// 绘图
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnPlot_Click(object sender, EventArgs e)
		{
            ClearChart();
            float[] value = new float[100];
            Random rand = new Random();
            for (int i = 0; i < 100; i++)
            {
                value[i] = rand.Next(1, 100);
            }
			//生成绘图数据
			List<double> xx, yy,yy2;
			xx = new List<double>();
			yy = new List<double>();
			yy2 = new List<double>();
			for (int i=0;i<100;i++)
			{
				//double t = i * 3 * Math.PI / 100;
                int t = i;
				xx.Add(t);
				yy.Add(value[t]);
				yy2.Add(Math.Cos(t));
			}

			//添加曲线对象
			for (int i = 0; i < 2; i++)
			{
				chart1.Series.Add("Y"+i);
				chart1.Series[i].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
			}
			//绘制曲线
			for (int i = 0; i < xx.Count; i++)
			{
				chart1.Series[0].Points.AddXY(xx[i], yy[i]);
				chart1.Series[1].Points.AddXY(xx[i], yy2[i]);
			}
			//设置曲线坐标、颜色
			chart1.ChartAreas[0].AxisY.Minimum = 0;
			chart1.ChartAreas[0].AxisY.Maximum = 100;
			chart1.ChartAreas[0].AxisX.Minimum = 0;
			chart1.Series[0].Color = System.Drawing.Color.Red;
			chart1.Series[1].Color = System.Drawing.Color.Green;		

		}

		private void btnClear_Click(object sender, EventArgs e)
		{
			ClearChart();
		}

        private void chart1_Click(object sender, EventArgs e)
        {

        }
	}
}
